package com.survivor.example;

import java.net.CookieHandler;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.survivor.activity.CoordHelper;
import com.survivor.opengl.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;

public class Lessone10Rendder implements Renderer {

	private float[] verCoord = { -1f, -1f, 0, 1f, -1f, 0, -1f, 1f, 0,
			1f, 1f, 0};

	private float[] texCoord = { 0, 1, 1, 1, 0, 0, 1, 0 };

	private FloatBuffer verBuffer, texBuffer;

	private int[] textures = new int[1];
	private Bitmap bmp;
	
	private CoordHelper ch;
	
	private int statu = 0;
	
	private static final int FREE = 1;
	private static final int MOVE = 2;
	

	public Lessone10Rendder(Context context) {
		// TODO Auto-generated constructor stub
		bmp = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.face);
		ch = new CoordHelper(context);
		verBuffer = makeFloatBuffer(verCoord);
		texBuffer = makeFloatBuffer(texCoord);
	}

	private FloatBuffer makeFloatBuffer(float[] src) {
		ByteBuffer bb = ByteBuffer.allocateDirect(src.length * 4);
		bb.order(ByteOrder.nativeOrder());

		FloatBuffer fb = bb.asFloatBuffer();
		fb.put(src);
		fb.position(0);

		return fb;
	}

	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		doDraw(gl);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		gl.glViewport(0, 0, width, height);
//		float ratio = (float) width / (float) height;
//		gl.glMatrixMode(GL10.GL_PROJECTION);
//		gl.glLoadIdentity();
//		//		
//		GLU.gluPerspective(gl, 45f, ratio, 1, 15);
//		gl.glMatrixMode(GL10.GL_MODELVIEW);
//		gl.glLoadIdentity();
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		loadTextures(gl);
	}

	private void loadTextures(GL10 gl) {
		gl.glClearColor(0, 0, 0, 1f);

		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glGenTextures(textures.length, textures, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);

		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
				GL10.GL_LINEAR);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
				GL10.GL_LINEAR);

		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glClearDepthf(1f);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glShadeModel(GL10.GL_SMOOTH);
	}

	private float moveX = 0;
	
	private void doDraw(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);

//		gl.glTranslatef(0, 0, -6);
		
		gl.glTranslatef(moveX, 0, 0); 

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		
//		gl.glLoadIdentity();
//		gl.glColor4f(1f, 1f, 1f, 1f);
//		gl.glPointSize(20f);
//		gl.glDrawArrays(GL10.GL_POINTS, 4, 1);
//		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		if(statu == FREE)
		{
			if(moveX < 0)
			{
				moveX += 0.05f;
				endX = moveX;
				if(moveX > 0)
					moveX = 0;
			}
			if(moveX > 0)
			{
				moveX -= 0.05f;
				endX = moveX;
				if(moveX < 0)
					moveX = 0;
			}
				
		}
		
	}
	
	private float startX = 0;
	private float endX = 0;
	
	public void onTouch(MotionEvent event)
	{
		int action = event.getAction();
		
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			statu = MOVE;
			startX = ch.scrX2OglX(event.getX());
			break;
		case MotionEvent.ACTION_MOVE:
//			System.out.println(event.getX() - startX);
			
//			System.out.println(ch.scrX2OglX(event.getX()) - startX);
			moveX = endX  +  ch.scrX2OglX(event.getX()) - startX;
//			System.out.println(ch.scrX2OglX(event.getX() - startX));
//			moveX = moveX + ch.scrX2OglX(endX  - startX);
			break;
		case MotionEvent.ACTION_UP:
		    endX = moveX;
		    statu = FREE;
			break;

		default:
			break;
		}
	}

}
